package com.htfyun.roobofactory.mediaUtils.playerutils;

import android.media.AudioManager;
import android.media.AudioTrack;
import android.util.Log;

/**
 * 音频播放实体
 * @author 奎华
 *
 */
public class MyAudioTrack {

    int mFrequency;					// 采样率

    int mChannel;					// 音频通道

    int mSampBit;					// 采样精度

    AudioTrack mAudioTrack;

    public MyAudioTrack(int frequency, int channel, int sampbit){
        mFrequency = frequency;
        mChannel = channel;
        mSampBit = sampbit;
    }

    /**
     * 初始化音频播放
     */
    public void init(){
        if (mAudioTrack != null){
            release();
        }
        // 获取最小缓存
        int minBufSize = AudioTrack.getMinBufferSize(mFrequency,
                                                    mChannel,
                                                    mSampBit);
        mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
                mFrequency,
                mChannel,
                mSampBit,
                minBufSize,
                AudioTrack.MODE_STREAM);
        mAudioTrack.play();
    }


    public void release(){
        if (mAudioTrack != null){
            mAudioTrack.stop();
            mAudioTrack.release();
        }
    }

    /**
     * 播放数据
     * @param data
     * @param offset
     * @param length
     */
    public void playAudioTrack(byte []data, int offset, int length){
        if (data == null || data.length == 0){
            return ;
        }

        try {
            mAudioTrack.write(data, offset, length);
        } catch (Exception e) {
            // TODO: handle exception
            Log.i("MyAudioTrack", "catch exception...");
        }
    }

    public int getPrimePlaySize(){
        int minBufSize =  AudioTrack.getMinBufferSize(mFrequency, mChannel, mSampBit);
        AudioTrack.getMinBufferSize(mFrequency,
                mChannel,
                mSampBit);

        return minBufSize * 2;
    }
}
